'use strict';
require('env2')('./.env')
const Hapi = require('@hapi/hapi');
const routesHello = require('./routes/happy');
const routesShops = require('./routes/shops');
const usersShops = require('./routes/users');
const config = require('./config');
// 引入自定义的 hapi-swagger 插件配置
const pluginHapiSwagger = require('./plugins/hapi-swagger');
const pluginHapiPagination = require('./plugins/hapi-pagination');
const hapiAuthJWT2 = require('hapi-auth-jwt2');
const pluginHapiAuthJWT2 = require('./plugins/hapi-auth-jwt2');

const server = Hapi.server({
    port: config.port,
    host: config.host,
});

server.route([
// 创建一个简单的 hello hapi 接口
...routesHello,
...routesShops,
...usersShops
]);

const init = async () => {

    // 注册插件
    await server.register([
        // 为系统使用 hapi-swagger
        ...pluginHapiSwagger,
        pluginHapiPagination,
        hapiAuthJWT2
    ]);

    pluginHapiAuthJWT2(server);

    await server.start();
    console.log(`Server running at: ${server.info.uri}`);
};

process.on('unhandledRejection', (err) => {

    console.log(err);
    process.exit(1);
});
init();